﻿@inherits LayoutComponentBase
@implements IDisposable

@inject Microsoft.AspNetCore.Components.NavigationManager NavigationManager
@inject IJSRuntime JSRuntime

@using Microsoft.JSInterop;

<nav class="top-row logo-container p-0 navbar navbar-dark fixed-top flex-nowrap">
    <button class="navbar-toggler d-block bg-primary border-0" @onclick="@ToggleNavMenu">
        <span class="navbar-toggler-icon"></span>
    </button>
    <span class="logo-image" @onclick="@(() => { NavigationManager.NavigateTo(NavigationManager.BaseUri); })" />
    <div class="demo-btn-container d-flex">
        <a class="download-btn navbar-toggler d-inline-block bg-primary text-white border-0" href="https://nuget.devexpress.com/" target="_blank" title="Obtain your NuGet feed URL">
            <span class="demo-download-icon"></span>
        </a>
        <div class="@($"bg-light text-dark d-inline-block theme-settings { ThemeSwitcherShown }")">
            <a class="nav-item nav-link" @onclick="@ToggleThemeSwitcherPanel" href="javascript:void(0);">
                <span class="demo-theme-icon"></span>
            </a>
        </div>
    </div>
</nav>

<div class="sidebar @NavMenuCssClass">
    <DxScrollView>
        <div>
            <NavMenu />
        </div>
    </DxScrollView>
    <div class="footer">
        <div class="info-wrapper">
            <div class="version">Version: 19.2.4</div>
            <div class="copyright">
                <span>Copyright &copy; 2013-</span><span>@(DateTime.Now.Year.ToString())</span>
                Weyhd.com Inc
            </div>
        </div>
    </div>
</div>

<div class="main">
    <div class="content px-4">
        @Body
    </div>
    <div class="footer">
        <div class="info-wrapper">
            <div class="version">Version: 19.2.4</div>
            <div class="copyright">
                <span>Copyright &copy; 2013-</span><span>@(DateTime.Now.Year.ToString())</span>
                weyhd.com Inc
            </div>
        </div>
    </div>
</div>

<ThemeSwitcher @bind-Shown="@ThemeSwitcherShown"></ThemeSwitcher>

@code {
    string NavMenuCssClass { get; set; } = "";

    void ToggleNavMenu()
    {
        NavMenuCssClass = string.IsNullOrEmpty(NavMenuCssClass) || NavMenuCssClass.Contains("d-none") ? "d-block d-xl-none" : "d-none d-xl-flex";
        ThemeSwitcherShown = false;
    }

    bool themeSwitcherShown = false;
    bool ThemeSwitcherShown {
        get => themeSwitcherShown;
        set {
            themeSwitcherShown = value;
            InvokeAsync(StateHasChanged); }
    }

    void ToggleThemeSwitcherPanel()
    {
        ThemeSwitcherShown = !ThemeSwitcherShown;
    }

    string UriFragment { get; set; } = "";

    void OnLocationChanged(object sender, LocationChangedEventArgs args)
    {
        if (!string.IsNullOrEmpty(NavMenuCssClass))
        {
            NavMenuCssClass = "";
            InvokeAsync(StateHasChanged);
        }
        JSRuntime.InvokeAsync<string>("ScrollToTarget", NavigationManager.ToAbsoluteUri(args.Location).Fragment);
    }

    protected override void OnInitialized()
    {
        base.OnInitialized();
        NavigationManager.LocationChanged += OnLocationChanged;
    }

    protected override Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            JSRuntime.InvokeAsync<string>("ScrollToTarget");
        }
        return base.OnAfterRenderAsync(firstRender);
    }

    void IDisposable.Dispose()
    {
        NavigationManager.LocationChanged -= OnLocationChanged;
    }
}
